home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / defdist.c < prev    next >
C/C++ Source or Header  |  1993-01-29  |  4KB  |  194 lines

  1. /*  $Revision: 1.6 $
  2. **
  3. */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <sys/types.h>
  7. #include <errno.h>
  8. #include "configdata.h"
  9. #include "libinn.h"
  10. #include "clibrary.h"
  11. #include "paths.h"
  12. #include "macros.h"
  13.  
  14.  
  15. typedef struct _DDENTRY {
  16.     char    *Pattern;
  17.     char    *Value;
  18.     int        Weight;
  19. } DDENTRY;
  20.  
  21. struct _DDHANDLE {
  22.     int        Count;
  23.     DDENTRY    *Entries;
  24.     DDENTRY    *Current;
  25. };
  26. typedef struct _DDHANDLE    DDHANDLE;
  27.  
  28.  
  29. extern FILE    *CA_listopen();
  30.  
  31. struct _DDHANDLE *
  32. DDstart(FromServer, ToServer)
  33.     FILE    *FromServer;
  34.     FILE    *ToServer;
  35. {
  36.     DDHANDLE    *h;
  37.     DDENTRY    *ep;
  38.     FILE    *F;
  39.     char    buff[BUFSIZ];
  40.     char    *p;
  41.     char    *q;
  42.     int        i;
  43.     char    name[sizeof _PATH_TEMPACTIVE + 10];
  44.  
  45.     /* Open the file. */
  46.     if ((F = fopen(_PATH_DISTPATS, "r")) != NULL)
  47.     name[0] = '\0';
  48.     else {
  49.     /* Not available locally; try remotely. */
  50.     if (FromServer == NULL || ToServer == NULL)
  51.         /* We're probably nnrpd running on the server and the
  52.          * file isn't installed.  Oh well. */
  53.         return NULL;
  54.     (void)strcpy(name, _PATH_TEMPACTIVE);
  55.     (void)mktemp(name);
  56.     if ((F = CA_listopen(name, FromServer, ToServer,
  57.             "distrib.pats")) == NULL)
  58.         return NULL;
  59.     }
  60.  
  61.     /* Count lines. */
  62.     for (i = 0; fgets(buff, sizeof buff, F) != NULL; i++)
  63.     continue;
  64.  
  65.     /* Allocate space for the handle. */
  66.     if ((h = NEW(DDHANDLE, 1)) == NULL) {
  67.     i = errno;
  68.     (void)fclose(F);
  69.     if (name[0])
  70.         (void)unlink(name);
  71.     errno = i;
  72.     return NULL;
  73.     }
  74.     h->Count = 0;
  75.     h->Current = NULL;
  76.     if ((h->Entries = NEW(DDENTRY, i)) == NULL) {
  77.     i = errno;
  78.     DISPOSE(h);
  79.     (void)fclose(F);
  80.     if (name[0])
  81.         (void)unlink(name);
  82.     errno = i;
  83.     return NULL;
  84.     }
  85.  
  86.     (void)fseek(F, (OFFSET_T)0, SEEK_SET);
  87.     for (ep = h->Entries; fgets(buff, sizeof buff, F) != NULL; ) {
  88.     if ((p = strchr(buff, '\n')) != NULL)
  89.         *p = '\0';
  90.     if (buff[0] == '\0' || buff[0] == '#')
  91.         continue;
  92.     if ((p = strchr(buff, ':')) == NULL
  93.      || (q = strchr(p + 1, ':')) == NULL)
  94.         continue;
  95.     *p++ = '\0';
  96.     ep->Weight = atoi(buff);
  97.     ep->Pattern = COPY(p);
  98.     q = strchr(ep->Pattern, ':');
  99.     *q++ = '\0';
  100.     ep->Value = q;
  101.     ep++;
  102.     }
  103.     h->Count = ep - h->Entries;
  104.  
  105.     (void)fclose(F);
  106.     if (name[0])
  107.     (void)unlink(name);
  108.     return h;
  109. }
  110.  
  111.  
  112. void
  113. DDcheck(h, group)
  114.     DDHANDLE    *h;
  115.     char    *group;
  116. {
  117.     DDENTRY    *ep;
  118.     int        i;
  119.     int        w;
  120.  
  121.     if (h == NULL || group == NULL)
  122.     return;
  123.  
  124.     w = h->Current ? h->Current->Weight : -1;
  125.     for (ep = h->Entries, i = h->Count; --i >= 0; ep++)
  126.     if (ep->Weight > w && wildmat(group, ep->Pattern)) {
  127.         h->Current = ep;
  128.         w = ep->Weight;
  129.     }
  130. }
  131.  
  132.  
  133. char *
  134. DDend(h)
  135.     DDHANDLE    *h;
  136. {
  137.     static char    NIL[] = "";
  138.     char    *p;
  139.     int        i;
  140.     DDENTRY    *ep;
  141.  
  142.     if (h == NULL) {
  143.     p = NIL;
  144.     return COPY(p);
  145.     }
  146.  
  147.     if (h->Current == NULL)
  148.     p = NIL;
  149.     else
  150.     p = h->Current->Value;
  151.     p = COPY(p);
  152.  
  153.     for (ep = h->Entries, i = h->Count; --i >= 0; ep++)
  154.     DISPOSE(ep->Pattern);
  155.     DISPOSE(h->Entries);
  156.     DISPOSE(h);
  157.     return p;
  158. }
  159.  
  160. #if    defined(TEST)
  161. int
  162. main(ac, av)
  163.     int            ac;
  164.     char        *av[];
  165. {
  166.     struct _DDHANDLE    *h;
  167.     char        *p;
  168.     FILE        *FromServer;
  169.     FILE        *ToServer;
  170.     char        buff[SMBUF];
  171.  
  172.     if (NNTPremoteopen(&FromServer, &ToServer, buff) < 0) {
  173.     if ((p = strchr(buff, '\n')) != NULL)
  174.         *p = '\0';
  175.     if ((p = strchr(buff, '\r')) != NULL)
  176.         *p = '\0';
  177.     if (buff[0])
  178.         (void)fprintf(stderr, "%s\n", buff);
  179.     else
  180.         perror("Can't connect");
  181.     exit(1);
  182.     }
  183.  
  184.     if ((h = DDstart(FromServer, ToServer)) == NULL)
  185.     perror("Init failed, proceeding anyway");
  186.     while ((p = *++av) != NULL)
  187.     DDcheck(h, p);
  188.     p = DDend(h);
  189.     printf(">%s<\n", p);
  190.     exit(0);
  191.     /* NOTREACHED */
  192. }
  193. #endif    /* defined(TEST) */
  194.